package mirrormonkey.rpc;

import mirrormonkey.rpc.tools.applications.ClientTestApplication;
import mirrormonkey.rpc.tools.applications.ServerTestApplication;
import mirrormonkey.rpc.tools.listeners.MakeVisibleListener;

import org.junit.After;
import org.junit.Before;

import com.jme3.network.HostedConnection;

public class SingleClientTestProto {

	public ServerTestApplication serverApplication;

	public ClientTestApplication clientApplication;

	public MakeVisibleListener visibilityListener;

	@Before
	public void setUp() throws Exception {
		serverApplication = new ServerTestApplication();
		serverApplication.start();
		clientApplication = new ClientTestApplication();
		clientApplication.start();

		visibilityListener = new MakeVisibleListener(this);
		clientApplication.appState.listeners.addListener(visibilityListener);
	}

	@After
	public void tearDown() throws Exception {
		clientApplication.stop();
		clientApplication = null;
		serverApplication.stop();
		serverApplication = null;
	}

	public HostedConnection getFirstConnection() {
		return serverApplication.server.getConnections().iterator().next();
	}

	public void processEvents() {
		serverApplication.appState.eventManager.processEvents();
		clientApplication.appState.eventManager.processEvents();
	}

	public void sleepNoInterrupt(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
		}
	}

}
